package com.rzico.sbl.ui.report.driver

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentReportDriverBinding
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.other.ManagerDecoration
import com.rzico.sbl.other.serviceId
import com.rzico.sbl.ui.MessageActivity
import com.rzico.sbl.ui.report.*
import com.rzico.sbl.ui.report.other.ReportService
import com.rzico.sbl.viewmodel.ReportViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.getString
import com.xinnuo.common.view.FullyGridLayoutManager
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.view.BadgeTextView
import com.xinnuo.slimadapter.SlimAdapter

class ReportDriverFragment : NavigationFragment() {

    private var mBinding: FragmentReportDriverBinding? = null

    override fun getViewModel() = get<ReportViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentReportDriverBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initLayout()
        initListener()

        mList.clear()
        mList.addItems(getViewModel().getDriverList(serviceId))
        mAdapter.setDataList(mList)
    }

    /**
     * 工作台
     */
    private fun initLayout() {
        mBinding?.let { binding ->
            binding.driverList.apply {
                isNestedScrollingEnabled = false
                layoutManager = FullyGridLayoutManager(getIContext(), 4)

                addItemDecoration(ManagerDecoration(vertical = 15))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerContent>(R.layout.item_manager_grid) { jector, bean, _ ->

                        // 解构声明
                        val (title, imageId, _, _) = bean

                        jector.apply {
                            text(R.id.manager_hint, title)

                            with<ImageView>(R.id.manager_image) {
                                if (imageId > 0) it.setImageResource(imageId)
                            }

                            with<BadgeTextView>(R.id.manager_num) {
                                it.setBadgeCount(bean.count)
                            }

                            clicked {
                                when (bean.title) {
                                    "出入库" -> startActivityEx<ReportDriverDepotActivity>()
                                    "水站" -> startActivityEx<ReportWaterShopActivity>()
                                    "商品统计" -> startActivityEx<ReportTotalActivity>()
                                    "库存统计" -> startActivityEx<DriverStockActivity>()
                                    "代金券统计" -> startActivityEx<DriverCouponActivity>()
                                    "水站统计" -> {
                                        when (serviceId) {
                                            in ReportService.MODE_DRIVER -> startActivityEx<DriverStationActivity>()
                                            else -> startActivityEx<DriverStationActivity>("mode" to "hide")
                                        }
                                    }
                                }
                            }
                        }
                    }
                    .attachTo(this)
            }

            binding.driverTitle.text = getString("shopName")
        }
    }

    private fun initListener() {
        mBinding?.apply {
            driverOrder.isVisible = serviceId in ReportService.MODE_DRIVER

            // 消息
            driverMsg.oneClick { startActivityEx<MessageActivity>() }

            // 订单
            driverOrderAll.oneClick { startActivityEx<ReportOrderActivity>() }
            driverPay.oneClick { startActivityEx<ReportOrderActivity>("index" to 1) }
            driverWait.oneClick { startActivityEx<ReportOrderActivity>("index" to 2) }
            driverSend.oneClick { startActivityEx<ReportOrderActivity>("index" to 4) }
            driverBack.oneClick { startActivityEx<ReportOrderActivity>("index" to 5) }

            // 运单
            driverWayAll.oneClick { startActivityEx<ReportDeliverActivity>() }
            driverConfirm.oneClick { startActivityEx<ReportDeliverActivity>("index" to 1) }
            driverPei.oneClick { startActivityEx<ReportDeliverActivity>("index" to 2) }
            driverDone.oneClick { startActivityEx<ReportDeliverActivity>("index" to 3) }
            driverEnd.oneClick { startActivityEx<ReportDeliverActivity>("index" to 4) }

            // 订单数
            getViewModel()
                .mOrderJsonEvent
                .observe(getLifecycleOwner()) {
                    driverPayHint.setBadgeCount(it.optNotNull("unpaid"))
                    driverWaitHint.setBadgeCount(it.optNotNull("unshipped"))
                    driverSendHint.setBadgeCount(it.optNotNull("shipped"))
                    driverBackHint.setBadgeCount(it.optNotNull("refund"))
                }

            // 运单数
            getViewModel()
                .mDeliverJsonEvent
                .observe(getLifecycleOwner()) {
                    driverConfirmHint.setBadgeCount(it.optNotNull("unconfirmed"))
                    driverPeiHint.setBadgeCount(it.optNotNull("confirmed"))
                    driverDoneHint.setBadgeCount(it.optNotNull("completed"))
                    driverEndHint.setBadgeCount(it.optNotNull("hope"))
                }
        }
    }

    override fun onStart() {
        super.onStart()
        getViewModel().getCount("1")
        getViewModel().getShipCount()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

}